<?php
class RolesService extends Service{

    
    /**
     * @param int id
     * <pre>
     * {"type":"integer"}
     * </pre>
     * @return void
     */
    public static function delete($id) {
        if (self::_invoke()) return self::_output();
        $role = Doctrine::getTable('Role')->find($id);
        $role->delete();
    }
    

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string"},
     *     "general":{"type":"boolean"},
     *     "permissions":{"type":"array"}
     *   }
     * }
     * </pre>
     * @return void
     */
    public static function edit($data) {
        if (self::_invoke()) return self::_output();
        $role = Doctrine::getTable('Role')->find($data->id);
        $role->name = $data->name;
        $role->description = $data->description;
        $role->general = $data->general;
        RoleTable::relatePermissions($role->id, $data->permissions);
        $role->save();
    }

    /**
     * @param object $data
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string"},
     *     "general":{"type":"boolean"}
     *   }
     * }
     * </pre>
     * @return int
     */
    public static function insert($data) {
        if (self::_invoke()) return self::_output();
        $conn = Doctrine_Manager::connection();
        $conn->beginTransaction();

        $role = new Role;
        $role->name = $data->name;
        $role->description = $data->description;
        $role->general = $data->general;
        $role->save();

        $conn->commit();
        return $role->id;
    }
    
    /**
     * @param int id
     * @return object
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string"},
     *     "general":{"type":"boolean"}
     *   }
     * }
     */
    public static function get($id) {
        $role = RoleTable::findWithPermissions($id);
        return $role;
    }

    /**
     * @param int boolean
     * @return object
     * <pre>
     * {
     *   "type":"object",
     *   "properties": {
     *     "id":{"type":"string"},
     *     "name":{"type":"string","minLength":5},
     *     "description":{"type":"string"},
     *     "general":{"type":"boolean"}
     *   }
     * }
     * </pre>
     */
    public static function getAll() {
        $roles = RoleTable::findAllWithPermissions();
        return $roles;
    }

    public static function getAllCategorized($loadPermissions = true) {
        $roles = Doctrine::getTable('Role')->findAll(Doctrine::HYDRATE_ARRAY);
        $arCategorized = array(
          'general'=>array(),
          'project'=>array()
        );
        foreach($roles as $role) {
            if($role['general']) {
                $arCategorized['general'][] = $role;
            }
            else {
                $arCategorized['project'][] = $role;
            }
        }
        return $arCategorized;
    }

    public static function getAllPermissions() {
        $permissions = Doctrine::getTable('Permission')->findAll(Doctrine::HYDRATE_ARRAY);
        return $permissions;
    }


}